package it.valeandre.omino.fragment;


import it.valeandre.omino.R;
import it.valeandre.omino.image.ImageDownloadService;
import it.valeandre.omino.image.ImageDownloadServiceListener;
import it.valeandre.omino.model.Item;
import it.valeandre.omino.utils.ScalableImageView;
import it.valeandre.omino.utils.Utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.TextView;

public class FeedsDetailFragment extends UpgradableBaseFragment {

	public static final String TAG = "FeedsDetailFragment";
	private View mContentView;
	private Item mFeed;
	private Bitmap mArticleImage;

	public FeedsDetailFragment(Item feed) {
		mFeed = feed;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.feeds_detail_fragment, container, false);

		// WebView webView = (WebView)
		// mContentView.findViewById(R.id.feeds_detail_webview);
		// webView.loadData(mHtml, "text/html", "UTF-8");
		TextView title = (TextView) mContentView.findViewById(R.id.feeds_detail_title);
		TextView date = (TextView) mContentView.findViewById(R.id.feeds_detail_date);
		TextView text = (TextView) mContentView.findViewById(R.id.feeds_detail_text);
		ScalableImageView image = (ScalableImageView) mContentView.findViewById(R.id.feeds_detail_image);

		String imageUrl = extractImageFromHtml(mFeed.description);
		downloadImage(imageUrl, image);
		
		title.setText(mFeed.title);
		
//		String dateString = Utils.convertLocale(mFeed.pubDate, Locale.UK, Locale.ITALIAN, "EEE, dd MMM yyyy hh:mm:ss z", "EEE, dd MMM yyyy");
		date.setText(mFeed.pubDate);
		text.setText(extractTextFromHtml(mFeed.description));

		return mContentView;
	}

	private String extractImageFromHtml(String html) {
		int start = html.indexOf("<img");
		int end = html.indexOf(".jpg") + 4;
		String imgTag = html.substring(start, end);
		Utils.log("Feed image tag = " + imgTag);
		start = imgTag.indexOf("href=\"") + 6;
		String imgUrl = imgTag.substring(start);
		Utils.log("Feed image URL = " + imgUrl);
		return imgUrl;
	}

	private String extractTextFromHtml(String html) {
		int start = html.indexOf("<p>");
		int end = html.indexOf("</p>") + 4;
		html = html.substring(start, end);
		start = html.indexOf("<a");
		end = html.indexOf("</a>") + 4;
		String replacement = html.substring(start, end);
		html = html.replace(replacement, "");
		html = html.replace("<p>", "");
		html = html.replace("</p>", "");
		return html;
	}

	private void downloadImage(String url, final ImageView image) {

		ImageDownloadService.getInstance().downloadImage(getActivity(), url, new ImageDownloadServiceListener() {

			@Override
			public void downloadImagePerformed(String url, final String filename) {

				// Log.d("OMINO", "Image downloaded url: " + url);
				mArticleImage = BitmapFactory.decodeFile(filename);

				mHandler.post(new Runnable() {
					public void run() {
						if (getActivity() != null) {
							Animation myFadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
							image.setImageBitmap(mArticleImage);
							myFadeInAnimation.setAnimationListener(new AnimationListener() {

								public void onAnimationStart(Animation animation) {
									image.setVisibility(View.VISIBLE);
								}

								public void onAnimationRepeat(Animation animation) {
								}

								public void onAnimationEnd(Animation animation) {
								}
							});
							image.startAnimation(myFadeInAnimation);
						}
					}
				});
			}

			@Override
			public void downloadImageFailed(String url, int statusCode) {
			}

			@Override
			public void downloadImageFailed(String url, Throwable reason) {
			}

		});
	}
	
	@Override
	public void onDetach() {
		super.onDetach();
		if (mArticleImage != null && !mArticleImage.isRecycled()) {
			mArticleImage.recycle();
		}
	}
	
	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}

}
